<template>
  <div class="enterprise-activity">
    <!-- 只有当数据加载完成且有数据时才渲染表格 -->
    <el-table
      v-if="!loading && enterPrizeLists.length"
      :data="enterPrizeLists"
      class="activity-table"
      :header-cell-style="{
        background: '#f5f7fa',
        color: '#333',
        fontWeight: 600,
        fontSize: '14px',
        height: '50px',
      }"
      :cell-style="{
        fontSize: '14px',
        padding: '16px 0',
      }"
    >
      <el-table-column align="center" label="企业活动">
        <template #default="{ row }">
          <div @click="toActivityDetail(row.id)" class="activity-title">
            {{ row.name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" width="150px" label="发布时间">
        <template #default="{ row }">
          <div class="publish-time">{{ row.createTime?.split(' ')[0] ?? '-' }}</div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 加载状态显示 -->
    <div v-else-if="loading" class="loading-container">
      <el-loading
        :element-loading-text="`加载中...`"
        :element-loading-spinner="svg"
        element-loading-svg-view-box="-10, -10, 50, 50"
        element-loading-background="rgba(122, 122, 122, 0.8)"
      />
    </div>

    <!-- 无数据状态 -->
    <el-empty v-else description="暂无数据" />

    <!-- 分页器只在有数据时显示 -->
    <el-pagination
      v-if="total > 0"
      background
      class="pagination-wrapper"
      layout="prev, pager, next"
      small
      :total="total"
      :page-size="query.size"
      v-model:current-page="query.page"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { listEnterpriseActivity } from '@/api/enterpeise';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const query = reactive({
  page: 1,
  size: 10,
});

const loaded = ref(false);
const loading = ref(true);
const enterPrizeLists = ref<any[]>([]);

const total = ref(0);

const svg = `
  <path class="path" d="
    M 30 15
    L 28 17
    M 25.61 25.61
    A 15 15, 0, 0, 1, 15 30
    A 15 15, 0, 1, 1, 27.99 7.5
    L 15 15
  " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
`;

onMounted(() => {
  loadData();
});

function loadData() {
  loading.value = true;
  listEnterpriseActivity(query).then((res: any) => {
    console.log('API响应:', res); // 打印响应内容
    if (res.code === 200) {
      enterPrizeLists.value = res.data?.records;
      total.value = res.data?.total;
    } else {
      ElMessage.error(res.msg);
    }
    loading.value = false;
    loaded.value = true; // 标记数据已加载完成
  });
}

const router = useRouter();
function toActivityDetail(id: string | number) {
  const { href } = router.resolve({
    name: 'EnterpriseActivityDetail',
    query: {
      id,
    },
  });
  window.open(href, '_blank');
}

function handleCurrentChange(val: number) {
  query.page = val;
  loadData();
}
</script>

<style lang="scss" scoped>
.enterprise-activity {
  width: 100%;
  padding: 40px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);

  .activity-table {
    :deep(.el-table__header) {
      th {
        border-bottom: 2px solid #dcdfe6;
      }
    }

    :deep(.el-table__row) {
      transition: all 0.3s ease;

      &:hover {
        background-color: #f5f7fa;
      }
    }
  }

  .activity-title {
    color: #409eff;
    cursor: pointer;
    transition: color 0.3s ease;
    font-weight: 500;
    padding: 8px 16px;

    &:hover {
      color: #66b1ff;
      text-decoration: underline;
    }
  }

  .publish-time {
    color: #606266;
    font-size: 13px;
  }

  .pagination-wrapper {
    margin-top: 20px;
    padding: 10px 0;
    display: flex;
    justify-content: flex-end;

    :deep(.el-pagination.is-background) {
      .el-pager li:not(.disabled) {
        &.active {
          background-color: #409eff;
          color: #fff;
        }

        &:hover {
          color: #409eff;
        }
      }
    }
  }
}
</style>
